<?php
require_once(PATH_TO_APP . "/components/debugger/Debugger.php");
class Layers
{  
    private $map_;
    private $arr_layers_ = array();
    private $arr_layer_properties_ = array();
    private $map_list_;
    private $request_;
    //private $debugger_;
    
    const PROPERTY_LAYER_OPACITY = 100;
    
    const PROPERTY_LAYER_FILL_COLOR = '#000000';
    
    const PROPERTY_LAYER_NO_FILLING = true;
    
    const PROPERTY_LAYER_OUTLINE_COLOR = '#000000';
    
    const PROPERTY_LAYER_OUTLINE_WIDTH = 1;
    
    const PROPERTY_LAYER_MAX_OUTLINE_WIDTH = 10;

    public function __construct(&$map, &$map_list, $request, $add_layers)
    {
        //$this->debugger_ = new Debugger();
        $this->map_ = $map;
        $this->map_list_ = $map_list;
        $this->request_ = $request;
        if($this->map_list_->get('arr_layers') != false)
            $this->arr_layers_ = $this->map_list_->get('arr_layers');
        if($this->map_list_->get('arr_layer_properties') != false)
            $this->arr_layer_properties_ = $this->map_list_->get('arr_layer_properties');
        if(count($this->request_->getArray('post')) > 0)
            $this->checkInput();
        $this->map_list_->set('arr_layers', $this->arr_layers_);
        $this->map_list_->set('arr_layer_properties', $this->arr_layer_properties_);
        if($add_layers)
            $this->addLayers();
    }
        
    public function __destruct(){}
    
    public function assignPropertiesToTemplate($output_handler, $layer)
    {
        if(!array_key_exists($layer, $this->arr_layer_properties_))
            $this->setLayer($layer);
        $output_handler->assignValuesToTemplate(array("arr_properties" => $this->getLayer($layer)));
        
    }
    
    private function checkInput()
    {
        if($this->request_->issetParameter('get', 'overlayupdate'))
        {
            $layer = $this->request_->getParameter('get', 'overlay');
            if($this->request_->issetParameter('post', 'layer_opacity'))
            {
                $layer_opacity = (int)$this->request_->getParameter('post', 'layer_opacity')>=0 && (int)$this->request_->getParameter('post', 'layer_opacity')<=constant("self::PROPERTY_LAYER_OPACITY") ? (int)$this->request_->getParameter('post', 'layer_opacity') : constant("self::PROPERTY_LAYER_OPACITY");
                $this->setLayerProperty($layer, 'layer_opacity', $layer_opacity);
            }
            if($this->request_->issetParameter('post', 'fill_color'))
            {
                $fill_color = $this->extractColor($this->request_->getParameter('post', 'fill_color'));
                if($fill_color !== false)
                    $this->setLayerProperty($layer, 'fill_color', $fill_color);
            }
            if($this->request_->issetParameter('post', 'no_filling'))
            {
                $this->setLayerProperty($layer, 'no_filling', true);
            }
            else
            {
                $this->setLayerProperty($layer, 'no_filling', false);
            }
            if($this->request_->issetParameter('post', 'outline_color'))
            {
                $outline_color = $this->extractColor($this->request_->getParameter('post', 'outline_color'));
                if($outline_color !== false)
                    $this->setLayerProperty($layer, 'outline_color', $outline_color);
            }
            if($this->request_->issetParameter('post', 'outline_width'))
            {
                $outline_width = (int)$this->request_->getParameter('post', 'outline_width')>=1 && (int)$this->request_->getParameter('post', 'outline_width')<=constant("self::PROPERTY_LAYER_MAX_OUTLINE_WIDTH") ? (int)$this->request_->getParameter('post', 'outline_width') : constant("self::PROPERTY_LAYER_OUTLINE_WIDTH");
                $this->setLayerProperty($layer, 'outline_width', $outline_width);
            }
        }
        else
        {
            $this->arr_layers_ = array();
            if($this->request_->issetParameter('post', 'arr_layers'))
            {
                foreach(array_reverse($this->request_->getParameter('post', 'arr_layers')) as $item)
                {
                    $this->arr_layers_[] = $item;
                    $this->setLayer($item);
                }
            }
        }
    }
    
    private function extractColor($color)
    {
        $tmp_color = preg_replace("/[^0-9A-Fa-f]/", '', $color);
        if(strlen($tmp_color) == 6)
            return $tmp_color;
        return false;
    }
    
    private function setLayerProperty($layer, $property, $value)
    {
        if(!array_key_exists($layer, $this->arr_layer_properties_))
            $this->setLayer($layer);
        if(array_key_exists($property, $this->arr_layer_properties_[$layer]))
            $this->arr_layer_properties_[$layer][$property] = $value;
    }
    
    private function getLayer($layer)
    {
        return $this->arr_layer_properties_[$layer];
        
    }
    
    private function setLayer($layer)
    {
        if(!array_key_exists($layer, $this->arr_layer_properties_))
        {
            $this->arr_layer_properties_[$layer]['layer_opacity'] = constant("self::PROPERTY_LAYER_OPACITY");
            $this->arr_layer_properties_[$layer]['fill_color'] = constant("self::PROPERTY_LAYER_FILL_COLOR");
            $this->arr_layer_properties_[$layer]['no_filling'] = constant("self::PROPERTY_LAYER_NO_FILLING");
            $this->arr_layer_properties_[$layer]['outline_color'] = constant("self::PROPERTY_LAYER_OUTLINE_COLOR");
            $this->arr_layer_properties_[$layer]['outline_width'] = constant("self::PROPERTY_LAYER_OUTLINE_WIDTH");
        }
    }
    
    private function addLayers()
    {
        foreach($this->arr_layers_ as $layer)
        {
            $new_layer = ms_newLayerObj($this->map_);
            $new_layer->set('type', MS_LAYER_POLYGON);
            $new_layer->setConnectionType(MS_POSTGIS);
            $new_layer->set('connection', MAPSERVER_CONNECTION_STRING);
            $new_layer->set('data', 'the_geom from '.$layer);
            $new_layer->set('status', MS_ON);
            $new_layer->set('name', $layer);
            $new_layer->set('opacity', $this->arr_layer_properties_[$layer]['layer_opacity']);
            $class_obj = ms_newClassObj($new_layer);
            $class_obj->set('name', $layer);
            if(!$this->arr_layer_properties_[$layer]['no_filling'])
            {
                $fill_style = ms_newStyleObj($class_obj);
                $fill_style->color->setRGB(hexdec(substr($this->arr_layer_properties_[$layer]['fill_color'], 0, 2)), hexdec(substr($this->arr_layer_properties_[$layer]['fill_color'], 2, 2)), hexdec(substr($this->arr_layer_properties_[$layer]['fill_color'], 4, 2)));  
            }
            $style = ms_newStyleObj($class_obj);
            $style->set('width', $this->arr_layer_properties_[$layer]['outline_width']);
            $style->set('antialias', true);
            $style->outlinecolor->setRGB(hexdec(substr($this->arr_layer_properties_[$layer]['outline_color'], 0, 2)), hexdec(substr($this->arr_layer_properties_[$layer]['outline_color'], 2, 2)), hexdec(substr($this->arr_layer_properties_[$layer]['outline_color'], 4, 2)));
        }
    }
}

?>
